<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>复制到剪贴板示例</title>
    <link rel="stylesheet" href="https://www.unpkg.com/ew-message/dist/ew-message.min.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }

        h1 {
            font-size: 24px;
            color: #333;
        }

        p {
            font-size: 16px;
            color: #555;
            margin-bottom: 20px;
        }

        textarea {
            width: 100%;
            height: 100px;
            font-size: 16px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
            resize: none;
            margin-bottom: 20px;
            text-align: center;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #45a049;
        }

        .message {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            margin-top: 20px;
        }

        .error {
            color: red;
        }

        .success {
            color: green;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>复制到剪贴板示例</h1>
        <p>在下方文本框输入内容，点击按钮即可将内容复制到剪贴板。</p>

        <textarea id="textToCopy" placeholder="输入您想复制的文本..."></textarea>

        <button type="button" id="copyBtn">复制文本</button>

        <div class="message" id="copyMessage">复制结果会在这里显示</div>
    </div>

    <script src="https://www.unpkg.com/ew-message/dist/ew-message.min.js"></script>
    <script>
        const copyToClipboardAsync = str => {
            if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
                return navigator.clipboard.writeText(str);
            }
            return Promise.reject('The Clipboard API is not available.');
        };

        document.getElementById('copyBtn').onclick = function () {
            const text = document.getElementById('textToCopy').value;
            if (!text) {
                return ewMessage.warning('请输入需要复制的内容');
            }
            copyToClipboard(text).then(() => {
                document.getElementById('copyMessage').textContent = '文本已复制到剪贴板！';
                document.getElementById('copyMessage').classList.remove('error');
                document.getElementById('copyMessage').classList.add('success');
            }).catch((err) => {
                document.getElementById('copyMessage').textContent = '复制失败，请重试。' + err;
                document.getElementById('copyMessage').classList.remove('success');
                document.getElementById('copyMessage').classList.add('error');
            })
        }

    </script>

</body>

</html>